<template>
  <el-dialog
    :title="userId ? '修改员工' : '新增员工'"
    width="480px"
    :visible="dialogVisible"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <!-- 表单接口 -->
    <div class="form-container">
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addFormRules"
        label-width="80px"
      >
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="登录账号" prop="userName">
          <el-input v-model="addForm.userName" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="addForm.phonenumber" />
        </el-form-item>
        <el-form-item label="分配角色" prop="roleId">
          <el-select v-model="addForm.roleId" placeholder="请选择角色">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="员工状态" prop="status">
          <el-radio-group v-model="addForm.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button
        size="mini"
        type="primary"
        @click="confirmAdd"
      >确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListAPI } from '@/api/System/role'
import {
  createEmployeeAPI,
  updateEmployeeAPI,
  getEmployeeDetailAPI
} from '@/api/System/employee'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    userId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      roleList: [], // 角色列表
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
        phonenumber: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
        roleId: [{ required: true, message: '请分配角色', trigger: 'blur' }],
        status: [{ required: true, message: '请选择员工状态', trigger: 'blur' }]
      }
    }
  },
  watch: {
    userId(val) {
      if (val) {
        this.getEmployeeDetail()
      }
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    // 获取员工详情
    async getEmployeeDetail() {
      const res = await getEmployeeDetailAPI(this.userId)
      this.addForm = res.data
    },
    // 提交表单按钮
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.userId) {
            delete this.addForm.createTime
            await updateEmployeeAPI(this.addForm)
            this.$message.success('修改员工成功')
            this.handleClose()
            this.$emit('getEmployeeList')
            return
          } else {
            await createEmployeeAPI(this.addForm)
            this.$message.success('添加员工成功')
            this.handleClose()
            this.$emit('getEmployeeList')
          }
        } else {
          return false
        }
      })
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data
    },
    // 关闭计费规则
    handleClose() {
      this.$refs.addForm.resetFields()
      this.addForm = {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      }
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>
